'use strict';

layui.define(['treeselect', 'helper', 'setter'], function(exports) {
  var $ = layui.jquery;
  var util = layui.helper;

  var MODULE_NAME = 'common';
  var BASE_PATH = window.$$path || '';

  // 全局业务型代码

  // 已办 /wfFinished/getWorkflowFinishedCountAllLoginUser.htm

  // 统一获取组织树
  layui.treeselect({
    elem: '*[lay-role="org"]',
    data: util.resolvePath(BASE_PATH, '/webDataDictController/getAllOrgTreeStr.json')
  });

  // 全局定时器
  $('*[lay-render]').each(function(index, item) {
    var $this = $(item);
    var url = $this.attr('lay-render');
    var delay = $this.attr('lay-render-delay') || 18e5; // 默认半小时获取一次
    var props = $this.attr('lay-render-response');
    var fetch = function(path) {
      clearTimeout($this.data('timer'));
      $.ajax({
        url        : path,
        dataType   : 'json',
        ifModified : true,
        contentType: 'application/json;charset=UTF-8',
        success    : function(response) {
          $this.html(response && response.success ? props ? (util.getKeyValue(response, props) || 0) : response.data : 0);
          $this.data('timer', setTimeout(function() {
            fetch(path);
          }, delay));
        },
        error: function() {
          clearTimeout($this.data('timer'));
        }
      });
    };

    url && fetch(util.resolvePath(BASE_PATH, url));
  });

  exports(MODULE_NAME, {});
});
